<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

  <script>
    function getData(url) {
      const pro = new Promise((reslove, reject) => {
        // 1.创建 xhr 对象
        let xhr = new XMLHttpRequest()
        // 2. 配置请求方式和地址
        xhr.open('GET', url)
        // 3. 发送请求
        xhr.send()
        // 4.接收结果
        xhr.onreadystatechange = function () {
          if (xhr.readyState !== 4) return
          if (xhr.readyState === 4 && xhr.status === 200) {
            reslove(xhr.responseText)
          } else {
            reject('服务端错误')
          }
        }
      })
      return pro
    }

    var p1 = getData('http://localhost:3000/data')

    p1.then((data) => {
      console.log(data)
    })

  </script>
</body>

</html>